"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callFunction = callFunction;
exports.callService = callService;
const appConfig_1 = require("../config/appConfig");
const localService_1 = require("./localService");
const SERVICE_NAME = "couplesService";
function callFunction(name, data) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!appConfig_1.appConfig.useCloud) {
            throw new Error("当前为本地模式，无法调用云函数");
        }
        const res = yield wx.cloud.callFunction({
            name,
            data,
        });
        const result = res.result;
        if (!result) {
            throw new Error("云函数返回为空");
        }
        if (result.code !== 200) {
            throw new Error(result.message || "云函数请求失败");
        }
        return result.data;
    });
}
function callService(action, data) {
    if (!appConfig_1.appConfig.useCloud) {
        return Promise.resolve((0, localService_1.callLocalService)(action, data || {}));
    }
    return callFunction(SERVICE_NAME, Object.assign({ action }, (data || {})));
}
